using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
using System.Data;

namespace TST.SharePoint.Shared.WebControls
{
    public class SharePointGantt : Control
    {
        private Exception _exception;
        private GridDatasource _datasource;
        private DatasourceConfiguration _configuration;
        private List<IFilterValues> _filterProviders;
        private Gantt _gantt;

        public SharePointGantt(GridDatasource datasource, DatasourceConfiguration configuration)
        {
            _filterProviders = new List<IFilterValues>();
            _datasource = datasource;
            _configuration = configuration;
        }

        public GridDatasource GridDatasource
        {
            get { return _datasource; }
            set
            {
                _datasource = value;
                ResetConfiguration();
            }
        }

        public DatasourceConfiguration Configuration
        {
            get { return _configuration; }
            set
            {
                _configuration = value;
                if (_datasource != null)
                {
                    _datasource.Configuration = _configuration;
                }
                ResetConfiguration();
            }
        }

        private void ResetConfiguration()
        {
        }

        /// <summary>
        /// Create the SharePoint grid. Uses the SharePoint SPGridView object.
        /// </summary>
        protected override void CreateChildControls()
        {
            SPList dummy = SPContext.Current.Web.Lists["pt"];
            _gantt = new Gantt("MyGantt", dummy, null, "Title", "StartDate", "DueDate", "PercentComplete");
            Controls.Add(_gantt);
        }

        protected override void OnInit(EventArgs args)
        {
            base.OnInit(args);
        }

        /// <summary>
        /// PreRender; databind the grid.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (GridDatasource != null)
            {
                try
                {
                    _gantt.DataSource = GridDatasource.GetData();
                    // Bind the grid to the datasource.
                    DataBind();
                }
                catch (Exception ex)
                {
                    // Message is rendered by the webpart. Prevents the SharePoint page from 'crashing'.
                    _exception = ex;
                }
            }
            base.OnPreRender(e);
        }

        private void SetFilters()
        {
            // Setup Filters
            if (FilterProviders.Count > 0)
            {
                foreach (IFilterValues filter in FilterProviders)
                {
                    if (filter != null && filter.ParameterValues != null &&
                        GridDatasource.Fields.GetFieldsWithDisplayName(filter.ParameterName) != null)
                    {
                        foreach (Field f in GridDatasource.Fields.GetFieldsWithDisplayName(filter.ParameterName))
                        {
                            TST.SharePoint.Shared.Filter newfilter = new Filter(f);
                            foreach (string val in filter.ParameterValues)
                            {
                                newfilter.FilterValues.Add(new FilterValue(val));
                            }
                            Configuration.Filters.Add(newfilter);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// If there has been an exception, render the message; otherwise render the grid.
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (_exception != null)
            {
                writer.WriteLine(string.Format("Error while loading the data: {0}<br>", _exception.Message));
                writer.WriteLine(string.Format("Exception: {0}", _exception.GetType().FullName));
                if (_exception.InnerException != null)
                {
                    writer.WriteLine(string.Format("Inner exception: {0}<br>", _exception.InnerException.Message));
                    writer.WriteLine(string.Format("Inner exception: {0}", _exception.InnerException.GetType().FullName));
                }
            }
            else
            {
                base.Render(writer);
            }
        }

        public List<IFilterValues> FilterProviders
        {
            get { return _filterProviders; }
            set { _filterProviders = value; }
        }

    }
}
